package com.docking.heylo.web.controller;

import com.docking.heylo.constant.SystemConstant;
import com.docking.heylo.dao.entity.Appointment;
import com.docking.heylo.dao.entity.Coupon;
import com.docking.heylo.exception.GeneralException;
import com.docking.heylo.service.IAppointmentService;
import com.docking.heylo.utils.HeyloMessageSource;
import com.docking.heylo.web.dto.AppointmentDto;
import com.docking.heylo.web.dto.OrderPayDto;
import com.docking.heylo.web.utils.ResultUtil;
import com.docking.heylo.web.vo.BaseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 约拍控制器
 *
 */
@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private HeyloMessageSource ms;

    @Autowired
    private IAppointmentService appointmentService;

    /****
     * 约拍费用配置
     *
     * @return
     */
    @PostMapping("/costConf")
    public BaseVo costConf(){
        List<Map<String, Object>> conf = appointmentService.costConf();

        Map<String, Object> result = new HashMap<>();
        for(Map<String, Object> map : conf){
            result.put(map.get("name").toString(), map.get("value"));
        }

        return ResultUtil.success(ms.getMessage(SystemConstant.success), result);
    }

    /***
     * 发起约拍
     *
     * @return
     */
    @PostMapping("/initiate")
    public BaseVo initiate(@RequestBody Appointment appointment){
        appointmentService.initiate(appointment);
        return ResultUtil.success(ms.getMessage("appointment.success"));
    }

    /***
     * 我的约拍
     *
     * @return
     */
    @PostMapping("/myOrders")
    public BaseVo orders(@RequestBody AppointmentDto appointmentDto){
        PageInfo<Map<String, Object>> orders = appointmentService.orders(appointmentDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success), orders);
    }

    /**
     * 摄影师接受订单
     *
     * @return
     */
    @PostMapping("/accept")
    public BaseVo accept(@RequestBody AppointmentDto dto){
        appointmentService.accept(dto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /**
     * 摄影师拒绝订单
     *
     * @return
     */
    @PostMapping("/refuse")
    public BaseVo refuse(@RequestBody AppointmentDto dto){
        appointmentService.refuse(dto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /***
     * 支付约拍
     *
     * @return
     */
    @PostMapping("/payAppointment")
    public BaseVo payAppointment(@RequestBody @Validated OrderPayDto orderPayDto){
        Object payInfo = appointmentService.payAppointment(orderPayDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success), payInfo);
    }

    /***
     * 开始约拍
     *
     * @return
     */
    @PostMapping("/start")
    public BaseVo start(@RequestBody AppointmentDto appointmentDto){
        appointmentService.start(appointmentDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /***
     * 约拍结束
     *
     * @param appointmentDto
     * @return
     */
    @PostMapping("/end")
    public BaseVo end(@RequestBody Appointment appointmentDto){
        appointmentService.endAppointment(appointmentDto);
        return ResultUtil.success(ms.getMessage(SystemConstant.success));
    }

    /***
     * 获取优惠券信息
     *
     * @return
     */
    @PostMapping("/coupon")
    public BaseVo coupon(@RequestBody Coupon coupon){
        Coupon dbCoupon = appointmentService.couponByCode(coupon.getPromoCode());
        if(dbCoupon == null){
            throw new GeneralException(ms.getMessage("promo_code.invalid"));
        }
        return ResultUtil.success(ms.getMessage(SystemConstant.success),dbCoupon);
    }

    /***
     * 申诉
     *
     * @return
     */
    @PostMapping("/appeal")
    public BaseVo appeal(@RequestBody Appointment appointment){
        appointmentService.appeal(appointment);
        return ResultUtil.success(ms.getMessage("appointment.appeal.success"));
    }




}
